package top.kaoshanji.q2ch16a.api;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import top.kaoshanji.q2ch16a.model.Spittle;

import java.util.HashMap;
import java.util.Map;

/**
 * @author kaoshanji
 * ^_^
 * create time 2025/4/26 10:52
 */
@Controller
@RequestMapping("/spittles")
public class SpittleApiController {

    /**
     * 代码清单-q2ch16a-437页a：在响应体中返回资源状态，使用@ResponseBody注解
     * @param spittleId
     * @return
     */
    @RequestMapping(value = "/spittleDetail/{spittleId}", method = RequestMethod.GET)
    @ResponseBody
    public Spittle spittleDetail(@PathVariable("spittleId") long spittleId) {
        Spittle spittle = new Spittle();

        spittle.setId(spittleId);
        spittle.setMessage("我是靓仔");

        return spittle;
    }

    /**
     * 代码清单-q2ch16a-438页a：在请求体中接收资源状态，使用@RequestBody注解
     * @param spittle
     * @return
     */
    @RequestMapping(value = "/saveSpittle", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, String> saveSpittle(@RequestBody Spittle spittle) {
        System.out.println("来自客户端的数据：" + spittle.getMessage());

        Map<String, String> map = new HashMap<>();
        map.put("userName", "我是靓仔");

        return map;
    }




}
